package com.youxin.busfoundation.bean;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.youxin.foundation.lang.BaseException;
import com.youxin.foundation.utils.StringUtils;

/**
 * 信息输出通用接口
 * 
 * @author yingde.cao
 *
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResponseObject {
	
	@JsonProperty("tracking_id")
	private long trackingId;
	
	private int code = BusinessMessages.SUCCESS_CODE;

	private String msg = BusinessMessages.SUCCESS_MESSAGE;
	
	private long timestamp = System.currentTimeMillis();
	
	public long getTrackingId() {
		return trackingId;
	}

	public void setTrackingId(long trackingId) {
		this.trackingId = trackingId;
	}

	/**
	 * 不输出到客户端
	 */
	@JsonIgnore
	private Object result;

	public ResponseObject() {
		this("");
	}

	public ResponseObject(Object data) {
		this.data = data;
	}

	public ResponseObject(ResponseMessage responseMessage) {
		this(responseMessage, "");
	}

	public ResponseObject(int code, String message) {
		this.code = code;
		this.msg = message;
	}

	public ResponseObject(ResponseMessage responseMessage, Object data) {
		this.code = responseMessage.getCode();
		this.msg = responseMessage.getMsg();
		this.data = data;
	}

	/**
	 * 系统异常错误返回
	 * 
	 * @param e
	 *            Exception
	 * @return ResponseObject
	 */
	public static ResponseObject systemError(Throwable e) {
		return systemError(e, null);	
	}
	
	public static ResponseObject userError(BaseException e) {
		return new ResponseObject(e.getCode(), e.getMsg());	
	}
	
	public static ResponseObject systemError(Throwable e,String prefix) {
		try {
			if(e instanceof BaseException)
			{
				BaseException exception=(BaseException)e;
				return userError(exception);
			}
			prefix=StringUtils.isNullOrEmpty(prefix)?"":prefix+":";
			ResponseMessage message = ResponseMessage.systemError(e);
			return ResponseObject.systemError(prefix+message.getMsg());
		} catch (Exception e2) {			
			return ResponseObject.systemError(e2.toString());
		}		
	}

	public static ResponseObject systemError(String msg) {
		
		return new ResponseObject(new ResponseMessage(BusinessMessages.FAILURE_CODE, msg));
	}

	public static ResponseObject successObject() {
		return new ResponseObject();
	}
	
	public static ResponseObject successObject(Object object) {
		return new ResponseObject(object);
	}

	/**
	 * 通用系统
	 * 
	 * @param msg
	 *            String 消息
	 * @return ResponseObject
	 */
	public static ResponseObject userFailure(String msg) {
		ResponseMessage message = ResponseMessage.userFailure(msg);
		return new ResponseObject(message);
	}
	
	public static ResponseObject userFailure(ResponseMessage message)
	{
		return new ResponseObject(message);
	}

	private Object data;

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	/**
	 * 是否成功的状态
	 * 
	 * @return
	 */
	public boolean success() {
		return this.code == BusinessMessages.SYSTEM_OK.getCode();
	}

	public long getTimestamp() {
		return timestamp;
	}

	public void setTimestamp(long timestamp) {
		this.timestamp = timestamp;
	}

	public Object getResult() {
		return result;
	}

	public void setResult(Object result) {
		this.result = result;
	}
	

}
